package com.panda.system.controller;

import com.panda.common.annotation.Log;
import com.panda.common.core.controller.BaseController;
import com.panda.common.core.domain.AjaxResult;
import com.panda.common.core.page.TableDataInfo;
import com.panda.common.enums.BusinessType;
import com.panda.common.utils.poi.ExcelUtil;
import com.panda.system.domain.TPandaTask;
import com.panda.system.service.ITPandaTaskService;
import com.panda.system.utils.TaskExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * 任务信息总Controller
 * 
 * @author panda
 * @date 2021-02-08
 */
@RestController
@RequestMapping("/system/task")
public class TPandaTaskController extends BaseController
{
    @Autowired
    private ITPandaTaskService tPandaTaskService;

    /**
     * 查询任务信息总列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TPandaTask tPandaTask)
    {
        startPage();
        List<TPandaTask> list = tPandaTaskService.selectTPandaTaskList(tPandaTask);
        return getDataTable(list);
    }

    /**
     * 导出任务信息总列表
     */
    @Log(title = "任务信息总", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TPandaTask tPandaTask)
    {
        List<TPandaTask> list = tPandaTaskService.selectTPandaTaskList(tPandaTask);
        ExcelUtil<TPandaTask> util = new ExcelUtil<TPandaTask>(TPandaTask.class);
        return util.exportExcel(list, "task");
    }

    /**
     * 获取任务信息总详细信息
     */
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId)
    {
        return AjaxResult.success(tPandaTaskService.selectTPandaTaskById(taskId));
    }

    /**
     * 新增任务信息总
     */
    @Log(title = "任务信息总", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPandaTask tPandaTask)
    {
        return tPandaTaskService.insertTPandaTask(tPandaTask);
    }

    /**
     * 修改任务信息总
     */
    @Log(title = "任务信息总", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPandaTask tPandaTask)
    {
        return toAjax(tPandaTaskService.updateTPandaTask(tPandaTask));
    }

    /**
     * 删除任务信息总
     */
    @Log(title = "任务信息总", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds)
    {
        return tPandaTaskService.deleteTPandaTaskByIds(taskIds);
    }

    /**
     * 下载任务信息列表模板
     */
    @Log(title = "任务信息", businessType = BusinessType.EXPORT)
    @GetMapping("/template")
    @ApiOperation("导出任务信息列表")
    public AjaxResult template(HttpServletResponse response)
    {
        ClassPathResource classPathResource = new ClassPathResource("exceltemp/task_temp.xlsx");
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        String filename = UUID.randomUUID().toString() + "_模板.xlsx";
        try {

            FileOutputStream out = new FileOutputStream(ExcelUtil.getAbsoluteFile(filename));
            InputStream inputStream = classPathResource.getInputStream();

            byte[] b = new byte[1024];
            int length;
            while ((length = inputStream.read(b)) > 0)
            {
                out.write(b, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AjaxResult.success(filename);
    }

    @Log(title = "任务信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ApiOperation("导入任务信息列表")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        TaskExcelUtil util = new TaskExcelUtil();
        TPandaTask task = util.importExcel(file.getInputStream());
        return tPandaTaskService.importTask(task);
    }
}
